// pages/chat/chat.js
const app = getApp();
var funcs = require('../../static/js/funcs.js');
Page({

  /**A
   * 页面的初始数据
   */
  data: {
    tishi: "暂未开放画像评测"
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {

  },
  select_group_button(e){
    let groupid = e.currentTarget.dataset.groupid;
    let displaystatus = e.currentTarget.dataset.displaystatus
    let openid = app.globalData.openid
    let that = this
    console.log("选中的组群为：" + groupid)
    this.setData({
      selectedgroup: groupid,
      displayFlag: (displaystatus==null)||(displaystatus.length<=0)? false:true
    })
    funcs.getGroupDetail(openid,groupid).then(res=>{
      this.setData({
        showtype: res.data.data.type,
        groupDetail:res.data.data
      })

      if(this.data.showtype==1){
        //刷新完成情况
        funcs.judgeDoneFlag(openid,groupid).then(res=>{
          if(res.data.succeed){
            console.log("完成情况：",res.data)
            that.setData({
              doneFlag: true
            })
          }else{
            that.setData({
              doneFlag: false
            })
          }

        })
      }

      console.log(res.data.data)
      console.log(this.data.showtype)
      this.fresh()
    })

  },

  fresh(){
    let openid = app.globalData.openid
    let that = this
    console.log(this.data.selectedgroup)
    funcs.getMember(openid,this.data.selectedgroup).then(res=>{

      if(res.data.succeed){
        let members = res.data.data.members
        console.log(members)
        that.setData({
          members:members
        })
      }else{
        that.setData({
          displayFlag:false,
          tishi:"不在本次参评范围内"
        })
      }
    })
  },


  doAssess(res){
    let data = res.currentTarget.dataset;
    console.log(data)
    var jsonString = JSON.stringify(data);
    //跳转页面并传递json字符串
    wx.navigateTo({
      url: '/pages/topiclist/topiclist?params=' + encodeURIComponent(jsonString)
    })
  },
  doAssess2(res){
    let data = res.currentTarget.dataset;
    console.log(data)
    var jsonString = JSON.stringify(data);
    //跳转页面并传递json字符串
    wx.navigateTo({
      url: '/pages/topiclist2/topiclist2?params=' + encodeURIComponent(jsonString)
    })
  },
  doReAssess(res){
    let data = res.currentTarget.dataset;
    console.log(data)
    var jsonString = JSON.stringify(data);
    //跳转页面并传递json字符串
    wx.showModal({
      title: '提示',
      content: '确定重新评价吗？',
      success(res) {
        if (res.confirm) {
          wx.navigateTo({
            url: '/pages/topiclist/topiclist?params=' + encodeURIComponent(jsonString)
          })
        }
      }
    })

  },

  doReAssess2(res){
    let data = res.currentTarget.dataset;
    console.log(data)
    var jsonString = JSON.stringify(data);
    //跳转页面并传递json字符串
    wx.showModal({
      title: '提示',
      content: '确定重新评价吗？',
      success(res) {
        if (res.confirm) {
          wx.navigateTo({
            url: '/pages/topiclist2/topiclist2?params=' + encodeURIComponent(jsonString)
          })
        }
      }
    })

  },

  getAssessDetail(res){
    let data = res.currentTarget.dataset;
    console.log(data)
    var jsonString = JSON.stringify(data);
    //跳转页面并传递json字符串
    wx.navigateTo({
      url: '/pages/topiclist/topiclist?params=' + encodeURIComponent(jsonString)
    })
  },

  getAssessDetail2(res){
    let data = res.currentTarget.dataset;
    console.log(data)
    var jsonString = JSON.stringify(data);
    //跳转页面并传递json字符串
    wx.navigateTo({
      url: '/pages/topiclist2/topiclist2?params=' + encodeURIComponent(jsonString)
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    let openid = app.globalData.openid
    if(app.globalData.loginedFlag==false){
      this.setData({
        loginedFlag: false
      })
      wx.reLaunch({
        url: '/pages/my/my',
      })
      wx.showToast({
        title: "请先绑定",
        icon: 'none',
        duration: 2000
      })
      return
    }
    funcs.judgeStaff(openid).then(res=>{
      if(res.data.succeed){
        console.log("该员工已注册")
        app.globalData.loginedFlag = true
        this.setData({
          loginedFlag: true
        })
        let that = this
        let openid = app.globalData.openid
        funcs.getGroup(openid).then(res=>{
          if(res.data.succeed){
            let grouplist = res.data.data.groups
            console.log(grouplist)
            if(grouplist.length>0){
              that.setData({
                errorFlag: false,
                grouplist: grouplist,
                selectedgroup: grouplist.length>0? grouplist[0].groupId: null,
                displayFlag: (grouplist[0].status==null)||(grouplist[0].status.length<=0)? false:true
              })  
              that.fresh()  
            }else{
              that.setData({
                errorFlag: true,
                tishi: "您所在群组，暂未开放画像评测功能"
              })
            }
          }
        })
      }else{
        app.globalData.loginedFlag = false
        this.setData({
          loginedFlag: false
        })
        wx.reLaunch({
          url: '/pages/my/my',
        })
        wx.showToast({
          title: "请先绑定",
          icon: 'none',
          duration: 2000
        })
      }
    })
    
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})